<template>
	<view>
		<u-sticky bgColor="#fff">
			<u-tabs lineColor="#3478F5" lineWidth="15" :scrollable="false" :activeStyle="{
		                 color: '#000000',
		                 fontWeight: 'bold',
		                 transform: 'scale(1.05)'
		             }" :inactiveStyle="{
		                 color: '#666666',
		                 transform: 'scale(1)'
		             }" :list="list1" @change="tabChage"></u-tabs>
			<template v-if="tabsId==1">
				<view class="flexbetween mainpadding_top ffffff" style="padding: 30rpx 70rpx;">
					<view class="flexcenter" @click="changescreen(1)">
						<view class="main_size bold" :class="sort_type==1?'lan_text':'hui_text'">好评</view>
						<view class="flexcolumn">
							<view class="checkshang" v-show="sort_type==1 && weigh==1"></view>
							<view class="nocheckshang" v-show="sort_type!=1 || (sort_type==1 && weigh==2)"></view>
							<view class="checkxia" style="margin-top: 5rpx;" v-show="sort_type==1 && weigh==2"></view>
							<view class="nocheckxia" style="margin-top: 5rpx;"
								v-show="sort_type!=1 || (sort_type==1 && weigh==1)"></view>
						</view>
					</view>
					<view class="flexcenter" @click="changescreen(3)">
						<view class="main_size bold" :class="sort_type==3?'lan_text':'hui_text'">收藏</view>
						<view class="flexcolumn">
							<view class="checkshang" v-show="sort_type==3 && weigh==1"></view>
							<view class="nocheckshang" v-show="sort_type!=3 || (sort_type==3 && weigh==2)"></view>
							<view class="checkxia" style="margin-top: 5rpx;" v-show="sort_type==3 && weigh==2"></view>
							<view class="nocheckxia" style="margin-top: 5rpx;"
								v-show="sort_type!=3 || (sort_type==3 && weigh==1)"></view>
						</view>
					</view>
					<view class="flexcenter" @click="changescreen(2)">
						<view class="main_size bold" :class="sort_type==2?'lan_text':'hui_text'">价格</view>
						<view class="flexcolumn">
							<view class="checkshang" v-show="sort_type==2 && weigh==1"></view>
							<view class="nocheckshang" v-show="sort_type!=2 || (sort_type==2 && weigh==2)"></view>
							<view class="checkxia" style="margin-top: 5rpx;" v-show="sort_type==2 && weigh==2"></view>
							<view class="nocheckxia" style="margin-top: 5rpx;"
								v-show="sort_type!=2 || (sort_type==2 && weigh==1)"></view>
						</view>
					</view>
				</view>
			</template>
		</u-sticky>
		<view class="flexcolumn" v-if="!list.data.length">
			<view class="placeholderimage"></view>
			<view class="margin_top xiaohui">暂无数据！</view>
		</view>
		<!-- 指定老师 -->
		<template v-if="tabsId==1">
			<view class="mainpadding2">
				<view class="mainpadding_top ffffff flexleft radius" v-for="item in list.data" :key="item.id"
					@click="jumpurl('/pages_index/personalHomepage?id=',item.id)">
					<view class="headimg" style="position: relative;">
						<image :src="item.avatar" class="headimg" mode=""></image>
						<view class="zaixian" v-if="item.status=='normal'"></view>
						<view class="lixian" v-else></view>
					</view>
					<view class="flexcolumnbet margin_left2" style="height: 120rpx;width: 100%;">
						<view class="flexbetween" style="width: 100%;">
							<view class="hei_text main_size bold">{{item.username}}</view>
							<view class="xiaohong">￥{{item.price}}/一次</view>
						</view>
						<view class="flexbetween" style="width: 100%;">
							<view class="xiaohui lianghang">{{item.description}}</view>
							<view class="tiwen xiaobai" @click.stop="jumpurl('/pages_askquestion/lijitw?id=',item.id)">
								立即提问</view>
						</view>
					</view>
				</view>
			</view>
		</template>
		<!-- 随机提问 -->
		<template v-if="tabsId==2">
			<view class="mainpadding2">
				<view class="mainpadding_top ffffff radius flexbetween"
					@click="jumpurl('/pages_askquestion/xuanzenj?type=',1)">
					<view class="titletext hei_text bold">年级</view>
					<view class="flexright">
						<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;"
							placeholder="请选择年级" disabled v-model="njInfo.name">
						<u-icon name="arrow-right" color="#999" size="12"></u-icon>
					</view>
				</view>
				<view class="mainpadding_top ffffff radius flexbetween" @click="selectxk">
					<view class="titletext hei_text bold">学科</view>
					<view class="flexright">
						<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;"
							placeholder="请选择学科" disabled v-model="xkmsg.name">
						<u-icon name="arrow-right" color="#999" size="12"></u-icon>
					</view>
				</view>
				<view class="mainpadding_top ffffff radius">
					<view class="titletext hei_text bold">描述</view>
					<textarea class="textareaclass margin_top hei_text main_size bold" placeholder="请输入描述"  v-model="content"/>
				</view>
				<view class="mainpadding_top ffffff radius">
					<view class="titletext hei_text bold">上传图片</view>
					<view class="flexleft">
						<view class="itemimgBox flexcenter margin_top" v-for="(item,index) in images_text" :key="index">
							<view class="listleftimg">
								<image class="listleftimg radius" :src="item" mode=""></image>
								<view class="deleteimg" @click="deleimage(index,images,images_text)"></view>
							</view>
						</view>
						<view class="itemimgBox flexcenter margin_top" v-if="images_text.length<4" @click="uploadImg(4,images,images_text)">
							<image class="listleftimg radius" src="../static/image/system/upimg.png" mode=""></image>
						</view>
					</view>
				</view>
				<view class="mainpadding_top ffffff radius">
					<view class="titletext hei_text bold">上传视频</view>
					<view class="videobox radius flexcenter margin_top" v-if="video_text.length">
						<video :src="video_text[0]" class="videobox radius"></video>
						<view class="deleteimg" @click="deleimage(0,video,video_text)"></view>
					</view>
					<view class="videobox radius flexcenter margin_top"  v-if="!video_text.length"  @click="upvideo(video,video_text)">
						<image class="listleftimg radius" src="@/static/image/system/upvideo.png" mode=""></image>
					</view>
				</view>
				<view class="mainpadding_top ffffff radius flexbetween">
					<view class="titletext bold hei_text">是否需要视频讲解</view>
					<u-radio-group v-model="is_need_video" placement="row" circle>
						<u-radio activeColor="#3478F5" :customStyle="{marginRight: '30rpx'}" label="否" name="0"></u-radio>
						<u-radio activeColor="#3478F5" label="是" name="1"></u-radio>
					</u-radio-group>
				</view>
				<view class="mainpadding_top ffffff radius flexbetween">
					<view class="titletext bold hei_text">奖励价格</view>
					<input type="text" style="text-align: right;" placeholder="请输入奖励价格" class="main_size bold hei_text" v-model="price">
				</view>
				<view class="ffffff mainpadding_top radius">
					<view class="titletext bold hei_text">接单后回答时间</view>
					<view class="flexbetween margin_top">
						<view class="lable" :class="duration==10?'itemgl':''" @click="duration=10">10分钟</view>
						<view class="lable" :class="duration==20?'itemgl':''" @click="duration=20">20分钟</view>
						<view class="lable" :class="duration==30?'itemgl':''" @click="duration=30">30分钟</view>
						<view class="lable" :class="duration==40?'itemgl':''" @click="duration=40">40分钟</view>
					</view>
				</view>
				<view class="submitbtn margin_top5" @click="submit">发布</view>
			</view>
		</template>
		<u-picker :show="xkshow" :columns="xkcolumns" title="选择学科" keyName="name" @cancel="xkshow=false" @confirm="xkconfirm"></u-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				list1: [{
					id: 1,
					name: '指定老师',
				}, {
					id: 2,
					name: '随机提问',
				}],
				tabsId: 1, //默认指定老师
				keywords: "",
				sort_type: "",
				weigh: "",
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				njInfo: {
					name:"",
					id:"",
					period:"",
				}, //本地存储的年级信息
				xkshow:false,
				xkcolumns:[],
				xkmsg:{
					id:"",
					name:"",
				},
				content:"",
				images:[],
				images_text:[],
				video:[],
				video_text:[],
				is_need_video:"0",
				duration:"10",
				price:"",
			}
		},
		onLoad() {
			this.init()
			this.getNjinfo()
		},
		onShow() {
			let _this = this
			uni.$off("checknj")
			uni.$on("checknj", data => {
				_this.njInfo = data
				console.log(_this.njInfo,"00000");
			})
		},
		watch: {
			"njInfo.id": {
				handler(newval) {
					if (newval != "") {
						this.getXkList()
					}
					this.xkmsg = {
						id:"",
						name:"",
					}
				},
				deep: true
			}
		},
		methods: {
			getNjinfo() { //获取本地选择的年级
				let njInfo = uni.getStorageSync("njInfo")
				if (njInfo) {
					this.njInfo = njInfo
				} else {
					this.njInfo = {
						name: "",
						id: "",
						period: "",
					}
				}
			},
			getXkList() { //获取学科数据
				httpRequest.request('/api/index/getsubject', 'GET', {
					education_grade_id: this.njInfo.id,
					period: this.njInfo.period,
				}).then(res => {
					this.xkcolumns = []
					this.xkcolumns.push(res.data)
				})
			},
			selectxk(){
				this.xkshow = true
			},
			xkconfirm(e){//学科确定
				this.xkmsg.name = e.value[0].name
				this.xkmsg.id = e.value[0].id
				this.xkshow = false
			},
			uploadImg(value, upImgs,upImgs_text) {//上传图片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs,upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs,upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg,upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
			upvideo(upvideo,upvideo_text) {//上传视频
				let uploadFile = ''
				let _this = this;
				// 1.选择要上传的视频
				uni.chooseVideo({
					maxDuration: 60, // 拍摄视频最长拍摄时间，单位秒。最长支持 60 秒。
					sourceType: ['album', 'camera'], // album 从相册选视频，camera 使用相机拍摄，默认为：['album', 'camera']
					success: function(res) {
						_this.uploadImageService(res.tempFilePath,upvideo,upvideo_text)
					}
				});
			},
			//刪除圖片
			deleimage(i, upimg,upimg_text) {
				upimg.splice(i, 1);
				upimg_text.splice(i, 1);
			},
			submit(){
				if (!httpRequest.checkIsLogin()) {
					uni.navigateTo({
						url: "/pages/login/login"
					})
					return false
				}
				if(this.content==""){
					httpRequest.toast("请输入问题描述")
					return false
				}
				if(this.njInfo.id==""){
					httpRequest.toast("请选择年级")
					return false
				}
				if(this.xkmsg.id==""){
					httpRequest.toast("请选择学科")
					return false
				}
				if(this.price==""){
					httpRequest.toast("请输入奖励价格")
					return false
				}
				httpRequest.request('/api/teacher/publish', 'POST', {
					education_grade_id:this.njInfo.id,
					education_subject_id:this.xkmsg.id,
					content:this.content,
					images:this.images.join(),
					video:this.video.join(),
					is_need_video:this.is_need_video,
					price:this.price,
					duration:this.duration,
				}, false, false, true).then(res => {
					if(res.code==1){
						this.xkmsg = {
							id:"",
							name:"",
						}
						this.content=""
						this.images=[]
						this.images_text=[]
						this.video=[]
						this.video_text=[]
						this.is_need_video="0"
						this.duration="10"
						this.price=""
						uni.navigateTo({
							url:"/pages_askquestion/wentizf?msg=" + encodeURIComponent(JSON.stringify(res.data))
						})
					}else{
						httpRequest.toast(res.msg)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			// 获取行业咨询列表
			init(isPage, page) {
				let _this = this;
				httpRequest.request('/api/teacher/getlist', 'GET', {
					page: page || 1,
					sort_type: this.sort_type,
					weigh: this.weigh
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			changescreen(val) {
				if (this.sort_type == val) {
					this.weigh = this.weigh == 1 ? 2 : 1
				} else {
					this.sort_type = val
					this.weigh = 1
				}
				this.init()
			},
			tabChage(e) {
				this.tabsId = e.id
				this.init()
			},
			jumpurl(url, id) {
				if (!httpRequest.checkIsLogin()) {
					uni.navigateTo({
						url: "/pages/login/login"
					})
					return false
				}
				uni.navigateTo({
					url: url + id
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	/deep/.u-radio-group {
		flex: none !important;
	}
</style>
<style scoped>
	.headimg {
		width: 120rpx;
		height: 120rpx;
		min-width: 120rpx;
	}

	.tiwen {
		max-width: 110rpx;
		min-width: 110rpx;
		text-align: center;
	}
</style>